package com.example.badminton.controller;

import com.example.badminton.common.AnonymousAccess;
import com.example.badminton.common.Result;
import com.example.badminton.entity.Banner;
import com.example.badminton.entity.Court;
import com.example.badminton.entity.News;
import com.example.badminton.entity.Venue;
import com.example.badminton.service.BannerService;
import com.example.badminton.service.CourtService;
import com.example.badminton.service.NewsService;
import com.example.badminton.service.VenueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/home")
@CrossOrigin
public class HomeController {
    
    @Autowired
    private BannerService bannerService;
    
    @Autowired
    private CourtService courtService;
    
    @Autowired
    private NewsService newsService;
    
    @Autowired
    private VenueService venueService;
    
    /**
     * 获取首页数据
     */
    @AnonymousAccess
    @GetMapping("/data")
    public Result<?> getHomeData() {
        try {
            Map<String, Object> data = new HashMap<>();
            
            // 获取轮播图
            List<Banner> banners = bannerService.getActiveBanners();
            data.put("banners", banners);
            
            // 获取热门场地（前3个）
            List<Court> hotCourts = courtService.list();
            if (hotCourts.size() > 3) {
                hotCourts = hotCourts.subList(0, 3);
            }
            data.put("hotCourts", hotCourts);
            
            // 获取最新资讯（前3个）
            List<News> latestNews = newsService.getNewsList("all", null);
            if (latestNews.size() > 3) {
                latestNews = latestNews.subList(0, 3);
            }
            data.put("latestNews", latestNews);
            
            // 获取场馆信息
            Venue venue = venueService.getVenueInfo();
            data.put("venue", venue);
            
            return Result.success(data);
        } catch (Exception e) {
            return Result.error("获取首页数据失败: " + e.getMessage());
        }
    }
} 